package com.fanyue.modules.wzb.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.fanyue.core.base.BaseServiceImpl;
import com.fanyue.core.exception.CoolPreconditions;
import com.fanyue.core.util.DatabaseDialectUtils;
import com.fanyue.modules.wzb.entity.ClothingSpecEntity;
import com.fanyue.modules.wzb.mapper.ClothingSpecMapper;
import com.fanyue.modules.wzb.service.ClothingSpecService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.tangzc.autotable.core.constants.DatabaseDialect;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.fanyue.modules.wzb.entity.table.ClothingInfoEntityTableDef.CLOTHING_INFO_ENTITY;
import static com.fanyue.modules.wzb.entity.table.ClothingSpecEntityTableDef.CLOTHING_SPEC_ENTITY;


/**
 * 系统用户
 */
@Service
@RequiredArgsConstructor
public class ClothingSpecServiceImpl extends BaseServiceImpl<ClothingSpecMapper, ClothingSpecEntity> implements ClothingSpecService {


    @Override
    public Object page(JSONObject requestParams, Page<ClothingSpecEntity> page, QueryWrapper qw) {
        String keyWord = requestParams.getStr("keyWord");
        Integer category = requestParams.getInt("category");
       
        String databaseDialect = DatabaseDialectUtils.getDatabaseDialect();
        if (databaseDialect.equals(DatabaseDialect.PostgreSQL)) {
            // 兼容postgresql
        } else {
            qw.select(CLOTHING_SPEC_ENTITY.ALL_COLUMNS);
        }
        qw.from(CLOTHING_SPEC_ENTITY);

        if (StrUtil.isNotEmpty(keyWord)) {
            qw.and(CLOTHING_SPEC_ENTITY.NAME.like(keyWord));
        }
        qw.and(CLOTHING_SPEC_ENTITY.CATEGORY.eq(category, category != null));
        
        if (databaseDialect.equals(DatabaseDialect.PostgreSQL)) {
            // 兼容postgresql
        } else {
            qw.groupBy(CLOTHING_SPEC_ENTITY.ID);
        }
        return mapper.paginate(page, qw);
    }

    @Override
    public Long add(JSONObject requestParams, ClothingSpecEntity entity) {
        ClothingSpecEntity check = getOne(
                QueryWrapper.create()
                        .eq(ClothingSpecEntity::getCategory, entity.getCategory())
                        .eq(ClothingSpecEntity::getName, entity.getName()));
        CoolPreconditions.check(check != null, "型号已存在");
        super.add(requestParams, entity);
        return entity.getId();
    }

    @Override
    public boolean update(JSONObject requestParams, ClothingSpecEntity entity) {
        ClothingSpecEntity check = getOne(
                QueryWrapper.create()
                        .eq(ClothingSpecEntity::getCategory, entity.getCategory())
                        .eq(ClothingSpecEntity::getName, entity.getName())
                        .ne(ClothingSpecEntity::getId, entity.getId()));
        CoolPreconditions.check(check != null, "型号已存在");
        return super.update(requestParams, entity);
    }

    @Override
    public Object list(JSONObject requestParams, QueryWrapper queryWrapper) {
        Integer category = requestParams.getInt("category");
        queryWrapper.from(CLOTHING_SPEC_ENTITY);

        if (category != null) {
            queryWrapper.and(CLOTHING_SPEC_ENTITY.CATEGORY.eq(category));
        }

        return mapper.selectListByQuery(queryWrapper);
    }

    @Override
    public List<ClothingSpecEntity> listByInfo(ClothingSpecEntity clothingSpec) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select(CLOTHING_SPEC_ENTITY.ALL_COLUMNS);
        queryWrapper.from(CLOTHING_SPEC_ENTITY);
        queryWrapper.and(CLOTHING_SPEC_ENTITY.CATEGORY.eq(clothingSpec.getCategory(), clothingSpec.getCategory() != null));
        return mapper.selectListByQuery(queryWrapper);
    }
}